package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduAssignmentDTO;
import com.zenith.edu.po.vo.EduAssignmentVO;
import com.zenith.edu.service.EduAssignmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/assignment")
@Tag(name = "作业管理", description = "作业管理接口")
public class EduAssignmentController {
    
    @Autowired
    private EduAssignmentService eduAssignmentService;
    
    @SecurePostMapping(value = "/createAssignment",roles = {"ADMIN"},permission = "assignment:create")
    @Operation(summary = "创建作业")
    public Result<EduAssignmentVO> createAssignment(@RequestBody EduAssignmentDTO dto) {
        EduAssignmentVO assignmentVO = eduAssignmentService.createAssignment(dto);
        return Result.success(assignmentVO);
    }
    
    @SecurePostMapping(value = "/getAssignmentById/{id}",roles = {"ADMIN"},permission = "assignment:retrieve")
    @Operation(summary = "根据ID获取作业详情")
    public Result<EduAssignmentVO> getAssignmentById(@PathVariable Long id) {
        EduAssignmentVO assignmentVO = eduAssignmentService.getAssignmentById(id);
        if (assignmentVO == null) {
            return Result.fail("作业不存在");
        }
        return Result.success(assignmentVO);
    }
    
    @SecurePostMapping(value = "/updateAssignment",roles = {"ADMIN"},permission = "assignment:update")
    @Operation(summary = "更新作业信息")
    public Result<EduAssignmentVO> updateAssignment(@RequestBody EduAssignmentDTO dto) {
        EduAssignmentVO assignmentVO = eduAssignmentService.updateAssignment(dto);
        return Result.success(assignmentVO);
    }
    
    @SecurePostMapping(value = "/deleteAssignment/{id}",roles = {"ROLE_ADMIN"},permission = "deleteAssignment")
    @Operation(summary = "删除指定作业")
    public Result<Void> deleteAssignment(@PathVariable Long id) {
        boolean result = eduAssignmentService.deleteAssignment(id);
        if (!result) {
            return Result.fail("作业删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getAssignmentList",roles = {"ADMIN"},permission = "assignment:retrieve")
    @Operation(summary = "获取作业列表")
    public Result<List<EduAssignmentVO>> getAssignmentList() {
        List<EduAssignmentVO> assignmentList = eduAssignmentService.getAssignmentList();
        return Result.success(assignmentList);
    }
}